package com.pp.controller;


import com.pp.bean.GovIndex;
import com.pp.bean.GovIndexImage;
import com.pp.service.GovIndexImageService;
import com.pp.util.Respons.ResponseUtil;
import com.pp.util.upload.UploadFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 首页图片 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2019-04-03
 */
@Controller
@RequestMapping("/govIndexImage")
@Api(value = "首页图片Controller", tags = {"首页图片类型类"})
public class GovIndexImageController {

    @Autowired
    GovIndexImageService govIndexImageService;


    @RequestMapping(value = "insert",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增首页图片")
    public Map insert(
            @RequestParam("width")Integer width,
            @RequestParam("height")Integer height,
            @RequestParam("file")MultipartFile file
            ){
        GovIndexImage govIndexImage = new GovIndexImage();
        govIndexImage.setHeight(height);
        govIndexImage.setWidth(width);
        GovIndex govIndex = new GovIndex();
        govIndexImage.setRelactionId(govIndex.getId());

        try {
            String url = UploadFile.fileUpload(file);
            govIndexImage.setUrl(url);
            System.out.println("url:"+url);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if(govIndexImageService.save(govIndexImage)){
            return ResponseUtil.getSuccessMap();
        }
        return ResponseUtil.getExceptionMap();
    }



    @RequestMapping(value = "select",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询首页图片")
    public List<GovIndexImage> select(){
        return govIndexImageService.list();
    }


}
